<template>
	<view class="content">
		<view class="evaluation-item">
			<view class="evaluation-product">
				<image :src="resource.coverImageUrl" mode="aspectFill"></image>
				<text class="desc">{{resource.resourceName}}</text>
			</view>
			<view class="evaluation-star">
				<view class="desc">
					<text>描述相符</text>
				</view>
				<view class="star">
					<image class='star-item' @click="changeCommentRank(index)"
						v-for="(item,index) in resourceComment.stars"
						:src="item? '../../static/image/redstar.png': '../../static/image/star.png'" />
				</view>
			</view>
			<view class="evaluation-comment">
				<textarea v-model="resourceComment.comment" placeholder="资源满足你的期望吗？说说它的优点和美中不足的地方吧" />
			</view>

		</view>
		<view class="button-base" @click="evaluate">发表</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {},
		data() {
			return {
				id: '',
				resource: {},
				resourceComment: {}
			}
		},
		onLoad(option) {
			this.id = option.id;
			this.inquiryResource(this.id);
			this.initResourceEvaluation();
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		methods: {
			//查询订单
			inquiryResource(id) {
				this.$api.request.inquiryResource({
					resourceUuid: id
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.resource = res.body.data;
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			initResourceEvaluation() {
				this.$set(this.resourceComment, 'stars', [5, 5, 5, 5, 5]);
				this.$set(this.resourceComment, 'commentRank', 5);
			},
			changeCommentRank(starIndex) {
				var starArray = [0, 0, 0, 0, 0];
				for (var i = 0; i <= 4; i++) {
					if (i <= starIndex) {
						starArray.splice(i, 1, 1);
					} else {
						starArray.splice(i, 1, 0);
					}
				}
				this.$set(this.resourceComment, 'stars', starArray);
				this.$set(this.resourceComment, 'commentRank', starIndex + 1);
			},
			evaluate() {
				let that = this;
				this.$api.request.commentResource({
					actionType: 'ADD',
					commentList: [{
						userDTO:{userUuid:this.userInfo.userUuid},
						resourceUuid: this.id,
						commentContent: this.resourceComment.comment,
						commentRank: this.resourceComment.commentRank
					}]
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.$api.msg('评论成功');
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/resource/evaluate-list?id='+this.id
							})
						}, 1000);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});

			}
		}

	}
</script>

<style lang="scss">
	page{
		background: $bg-color-light;
	}
	.content{
		margin: 0 $page-row-spacing;
	}

	.evaluation-item {
		background: #fff;
		margin-top: 20upx;
		padding: 10upx;

		.evaluation-product {
			width: 100%;

			image {
				float: left;
				height: 70upx;
				width: 70upx;
			}

			.desc {
				display: flex;
				color: $font-color-dark;
				font-size: 28upx;
				height: 70upx;
				line-height: 70upx;
				white-space: 'nowrap';
				overflow: hidden;
				text-overflow: ellipsis;
				padding-left: 20upx;
			}
		}

		.evaluation-star {
			margin-top: 20upx;
			display: flex;

			.desc {
				color: $font-color-base;
				font-size: 30upx;
				height: 50upx;
				line-height: 50upx;
			}

			.star {
				padding-left: 20upx;

				.star-item {
					width: 50upx;
					height: 50upx;
				}
			}
		}

		.evaluation-comment {
			margin-top: 20upx;

			textarea {
				padding: 10upx;
				font-size: 28upx;
			}
		}
	}
</style>